package com.xitang.system.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xitang.common.bean.BizCategory;
import com.xitang.common.bean.BizCategoryProduct;
import com.xitang.common.bean.BizMaterial;
import com.xitang.common.bean.BizProduct;
import com.xitang.common.bean.BizProductDetail;
import com.xitang.common.bean.BizProductMaterial;
import com.xitang.common.mapper.BizCategoryMapper;
import com.xitang.common.mapper.BizCategoryProductMapper;
import com.xitang.common.mapper.BizMaterialMapper;
import com.xitang.common.mapper.BizProductMapper;
import com.xitang.common.mapper.BizProductMaterialMapper;
import com.xitang.common.service.impl.ProductServiceImpl;
import com.xitang.common.util.JacksonUtil;
import com.xitang.system.poi.BizCategoryExcelListener;
import com.xitang.system.poi.BizCategoryProductExcelListener;
import com.xitang.system.poi.BizMaterialExcelListener;
import com.xitang.system.poi.BizProductExcelListener;
import com.xitang.system.poi.BizProductMaterialExcelListener;
import com.xitang.system.service.ISystemProductService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.File;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class SystemProductServiceImpl extends ProductServiceImpl implements ISystemProductService {
    @Autowired
    private BizCategoryMapper categoryMapper;

    @Autowired
    protected BizProductMaterialMapper productMaterialMapper;

    @Autowired
    protected BizMaterialMapper materialMapper;

    @Autowired
    private BizProductMapper productMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void importProduct(File file) {
        EasyExcel.read(file, BizCategory.class, new BizCategoryExcelListener(categoryMapper)).sheet("分类").doRead();
        EasyExcel.read(file, BizProduct.class, new BizProductExcelListener(productMapper)).sheet("产品").doRead();
        EasyExcel.read(file, BizMaterial.class, new BizMaterialExcelListener(materialMapper)).sheet("材料").doRead();
        EasyExcel.read(file, BizCategoryProduct.class, new BizCategoryProductExcelListener(categoryProductMapper)).sheet("分类产品关联").doRead();
        EasyExcel.read(file, BizProductMaterial.class, new BizProductMaterialExcelListener(productMaterialMapper)).sheet("产品材料关联").doRead();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteProduct(String productCode) {
        // 产品、产品材料关联、分类产品关联
        productMapper.deleteById(productCode);
        QueryWrapper<BizProductMaterial> productMaterialQueryWrapper = new QueryWrapper<>();
        productMaterialQueryWrapper.eq("product_code", productCode);
        productMaterialMapper.delete(productMaterialQueryWrapper);
        QueryWrapper<BizCategoryProduct> categoryProductQueryWrapper = new QueryWrapper<>();
        categoryProductQueryWrapper.eq("product_code", productCode);
        categoryProductMapper.delete(categoryProductQueryWrapper);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveProductDetail(BizProductDetail productDetail) {
        BizProduct product = JacksonUtil.jsonStringToBean(JacksonUtil.toJsonString(productDetail), BizProduct.class);
        productMapper.insert(product);
        if (StringUtils.isNotEmpty(productDetail.getCategoryCode())) {
            QueryWrapper<BizCategoryProduct> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("product_code", productDetail.getProductCode());
            categoryProductMapper.delete(queryWrapper);
            categoryProductMapper.insert(new BizCategoryProduct(productDetail.getCategoryCode(), productDetail.getProductCode()));
        }
        List<BizMaterial> materialList = productDetail.getMaterialList();
        if (CollectionUtils.isNotEmpty(materialList)) {
            QueryWrapper<BizProductMaterial> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("product_code", productDetail.getProductCode());
            productMaterialMapper.delete(queryWrapper);
            materialList.forEach((material) -> productMaterialMapper.insert(new BizProductMaterial(productDetail.getProductCode(), material.getMaterialCode())));
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateProductDetail(BizProductDetail productDetail) {
        BizProduct product = JacksonUtil.jsonStringToBean(JacksonUtil.toJsonString(productDetail), BizProduct.class);
        productMapper.updateById(product);
        if (StringUtils.isNotEmpty(productDetail.getCategoryCode())) {
            QueryWrapper<BizCategoryProduct> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("product_code", productDetail.getProductCode());
            categoryProductMapper.delete(queryWrapper);
            categoryProductMapper.insert(new BizCategoryProduct(productDetail.getCategoryCode(), productDetail.getProductCode()));
        }
        List<BizMaterial> materialList = productDetail.getMaterialList();
        if (CollectionUtils.isNotEmpty(materialList)) {
            QueryWrapper<BizProductMaterial> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("product_code", productDetail.getProductCode());
            productMaterialMapper.delete(queryWrapper);
            Set<String> materialSet = materialList.stream().map(BizMaterial::getMaterialCode).collect(Collectors.toSet());
            materialSet.forEach((materialCode) -> productMaterialMapper.insert(new BizProductMaterial(productDetail.getProductCode(), materialCode)));
        }
    }
}
